<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/4
 * Time: 17:04
 */

namespace app\admin\controller;
use app\admin\controller\common\Yan_zheng;
use think\Db;

use app\admin\controller\common\Admin;

class User extends Admin
{
    //检测管理组和小学是否存在
    public function is_admin_group()
    {
        $error = '';/*提示信息*/
        $type = 'y';/*是否可以使用*/
        $name = input('post.param');
        $data = Db::table('bg_usergroup')->where('name',$name)->count();
        if ($data != 0) {
            $error = '管理组已存在';
            $type = 'n';/*是否可以使用*/
        }
        else{
            $data = Db::table('q_datalist')->where('school',$name)->count();
            if ($data == 0) {
                $error = '项目不存在';
                $type = 'n';/*是否可以使用*/
            }
        }
        return ['info'=>$error,'status'=>$type];
    }
    #管理员权限页面
    public function admin_set()
    {

        $Yan_zheng = new Yan_zheng();
        #添加删除权限
        if(request() ->isAjax()){
            #验证字
            if(!$error = $Yan_zheng ->is_number([input('post.id'),input('post.type'),input('post.user')])){
                return $error;
            }
            #判断是删除 换是添加
            if(input('type') == 1){
                $da['user_id'] = input('post.user');
                $da['menu_id'] = input('post.id');
                return Db::table('bg_userandmenu')->insert($da);
            }
            else{
                return Db::table('bg_userandmenu')
                    ->where('menu_id',input('post.id'))
                    ->where('user_id',input('post.user'))
                    ->delete();
            }

        }

        #页面数据
        if(!$error = $Yan_zheng ->is_number([input('id')])){
            return $error;
        }
        $vi['url'] = Db::table('bg_userandmenu')->where('user_id',input('id'))->select();
        $vi['url'] = json_encode($vi['url']);
        $vi['id'] = input('id');
        return view('',$vi);
    }
    #修改管理员密码
    public function update_paw()
    {
        $Yan_zhegn = new Yan_zheng();
        if (!$error = $Yan_zhegn ->is_string([$_POST['id'],$_POST['paw']])) {
            return $error;
        }

        $state = Db::table('bg_admin')
            ->where('id',$_POST['id'])
            ->update(['password'=>md5($_POST['paw'])]);
        $Yan_zhegn ->rizhi("修改管理员密码，状态：{$state}",$_POST['id']);
        return $state;


    }
    #管理员列表
    public function index()
    {
        if(request() ->isAjax()){
            /*验证数据*/
            $Yan_zheng = new Yan_zheng();
            if(!$error = $Yan_zheng ->is_string([$_POST['name']])){
                return $error;
            }
            /*返回对应数据*/
            if($_POST['name'] == 'group'){
                return Db::table('bg_usergroup')->select();
            }
            else{
                return Db::table('bg_admin') ->where('status',1) ->where('groupId',$_POST['name']) ->select();
            }
        }
        return view();
    }

    #添加管理员、会员组
    public function add_admin()
    {
        if(request() ->isAjax()){
            $Yan_zheng = new Yan_zheng();
            #添加管理员
            if(isset($_POST['password'])){
                $da['groupId'] = $_POST['groupId'];
                $da['account'] = $_POST['account'];
                $da['password'] = md5($_POST['password']);
                $da['name'] = $_POST['name'];
                $da['remark'] = $_POST['remark'];
                if(!$error = $Yan_zheng ->is_string(explode(',,',implode(',,',$da)))){
                    return $error;
                }

                $state = Db::table('bg_admin')->insert($da);
                $Yan_zheng ->rizhi("添加后台管理员，状态：{$state}");
                return $state;
            }
            else{#添加用户组
                $da['name'] = $_POST['name'];
                $da['remark'] = $_POST['remark'];
                if(!$error = $Yan_zheng ->is_string(explode(',,',implode(',,',$da)))){
                    return $error;
                }
                $state =  Db::table('bg_usergroup')->insert($da);
                $Yan_zheng ->rizhi("添加管理员组，状态：{$state}");
                return $state;
            }
        }
        return view();
    }

    #验证管理员账号是否已存在
    public function is_admin()
    {
        $state = Db::table('bg_admin')->where('account',$_POST['param'])->count();
        if($state == 1){
            return ['info'=>'管理员已存在','status'=>'n'];
        }
        else{
            return ['info'=>'','status'=>'y'];
        }
    }

    #删除用户组
    public function delete_admin()
    {
        $Yan_zheng = new Yan_zheng();
        #验证数字

        if(isset($_POST['group'])){
            if(!$error = $Yan_zheng ->is_number([$_POST['group']])){
                return $error;
            }
            Db::table('bg_admin') ->where('groupId',$_POST['group']) ->update(['status'=>0]);
            $state =  Db::table('bg_usergroup') ->delete($_POST['group']);
            $Yan_zheng ->rizhi("删除管理员组以及下面的管理员，状态：{$state}",$_POST['group']);
            return $state;
        }
        else{

            if(!$error = $Yan_zheng ->is_number([$_POST['admin']])){
                return $error;
            }
            $state = Db::table('bg_admin') ->where('id',$_POST['admin']) ->update(['status'=>0]);
            $Yan_zheng ->rizhi("删除管理员，状态：{$state}",$_POST['admin']);
            return $state;
        }
    }

}